(function () {

    const $ajax = function (url, success, error, async=true) {
        this.url = url;
        this.type = "post";
        this.data = undefined;
        this.dataType = "json";
        this.async = async;
        this.success = success;
        this.error = error;
        this.encrypt = false;
        this.encryptKey = 'encrypt';
    };

    $ajax.prototype = {
        start : function () {
            const me = this;

            /*if (this.url.indexOf("?") == -1) {
                this.url = this.url + "?t=" + new Date().getTime();
            } else {
                this.url = this.url + "&t=" + new Date().getTime();
            }*/
            const key = this.encryptKey
            let data = this.data

            if (data) {
                if (this.type === 'post' || this.type === 'POST') {
                    if (this.encrypt) {
                        data = new Encrypt().encrypt(JSON.stringify(this.data))
                        data = JSON.stringify({[key]: data})
                    } else {
                        data = JSON.stringify(data)
                    }
                } else {
                    if (this.encrypt) {
                        data = {[key]: new Encrypt().encrypt(data)}
                    }
                }
            } else {
                data = {}
            }

            $.ajax({
                type: this.type,
                url: this.url,
                contentType: (this.type === 'post' || this.type === 'POST') ?
                    'application/json; charset=utf-8' :
                    'application/x-www-form-urlencoded',
                dataType: this.dataType,
                async: this.async,
                data,
                beforeSend: function(data) {

                },
                success: function(data) {
                    if (me.encrypt) {
                        data = new Encrypt().decrypt(data)
                        data = JSON.parse(data)
                    }
                    if(data && data.code && data.code != 200){
                        console.error(data['msg']);
                    }else {
                        me.success(data['result']);
                    }
                },
                error: function(error) {
                    me.error({status: error.status, statusText: error.statusText});
                }
            });
        },

        set : function (key, value) {
            if (typeof key == "object") {
                for (let i in key) {
                    if (typeof i !== "function")
                        this.data[i] = key[i];
                }
            } else {
                this.data[key] = (typeof value == "undefined") ? $("#" + key).val() : value;
            }
            return this;
        },

        setData : function(data){
            this.data = data;
            return this;
        },

        clear : function () {
            this.data = {};
            return this;
        },
        setType: function(type){
            this.type = type;
            return this;
        },
        setEncrypt: function (encrypt) {
            this.encrypt = encrypt;
            return this;
        },
        setEncryptKey: function (encryptKey) {
            this.encryptKey = encryptKey;
            return this;
        }
    };

    window.$ajax = $ajax;

}())